package com.mingzhi.blog.model;

import com.mingzhi.blog.enmus.ResultCodeEnum;
import lombok.Data;

@Data
public class Result<T> {
    //返回业务状态码
    //因为状态码少，使用枚举类
    private int code;
    //返回业务错误信息
    private String errMsg;
    //业务返回的数据
    private T data;

    //封装方法
    //成功的话
    public static <T> Result success(T data){
        Result result = new Result();
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setData(data);
        return result;
    }

    //失败的话
    public static <T> Result fail(String errMsg){
        Result result = new Result();
        result.setCode(ResultCodeEnum.FATL.getCode());
        result.setErrMsg(errMsg);
        return result;
    }

    public static <T> Result fail(String errMsg,T data){
        Result result = new Result();
        result.setCode(ResultCodeEnum.FATL.getCode());
        result.setErrMsg(errMsg);
        result.setData(data);
        return result;
    }

    public static <T> Result fail(int code,String errMsg){
        Result result = new Result();
        result.setCode(code);
        result.setErrMsg(errMsg);
        return result;
    }

    public static <T> Result fail(int code,String errMsg,T data){
        Result result = new Result();
        result.setCode(code);
        result.setErrMsg(errMsg);
        result.setData(data);
        return result;
    }

    public static <T> Result nologin(){
        Result result = new Result();
        result.setCode(ResultCodeEnum.NO_LOGION.getCode());
        result.setErrMsg("未登录");
        return result;
    }
 }
